package com.example.springboot.controller;

import cn.hutool.core.date.DateUtil;
import com.example.springboot.common.Result;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.springboot.entity.Goods;
import com.example.springboot.entity.Notice;
import com.example.springboot.entity.User;
import com.example.springboot.exception.ServiceException;
import com.example.springboot.service.IGoodsService;
import com.example.springboot.utils.TokenUtils;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import com.example.springboot.service.IOrdersService;
import com.example.springboot.entity.Orders;

import org.springframework.web.bind.annotation.RestController;
/**
 * <p>
 *  前端控制器
 * </p>
 * baciou8966@sandbox.com
 * @author wgc
 * @since 2023-09-03
 */
@RestController
@RequestMapping("/orders")
        public class OrdersController {
    
    @Resource
    private IOrdersService ordersService;

    @Resource
    private IGoodsService goodsService;

    //新增和修改
    @PostMapping
    public Result save(@RequestBody Orders orders) {  //@RequestBody接收前端数据
            //新增或者更新
        return Result.success(ordersService.saveOrUpdate(orders));
    }

    //新增或者更新
    @PostMapping("/{goodsId}/{num}")
    public Result createOrder(@PathVariable Integer goodsId, @PathVariable Integer num) {  //@RequestBody接收前端数据

        Goods goods = goodsService.getById(goodsId);
        if(goods == null){
            throw new ServiceException("-1","未找到商品");
        }

        User currentUser = TokenUtils.getCurrentUser();
        Integer userId = currentUser.getId();

        Orders orders = new Orders();

        orders.setName(goods.getName());
        orders.setNo(DateUtil.format(new Date(),"yyyyMMdd")+ System.currentTimeMillis());
        orders.setTime(DateUtil.now());
        orders.setUserId(userId);
        // 求总价，商品单价*商品数量
        orders.setTotal(goods.getPrice().multiply(BigDecimal.valueOf(num)));
        ordersService.saveOrUpdate(orders);
        return Result.success();
    }

    //查询所有数据
    @GetMapping()
    public Result findAll() {
        return Result.success(ordersService.list());
    }

    //删除
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
         return Result.success(ordersService.removeById(id));
    }
    //批量删除
    @PostMapping("/del/batch")
    public Result deleteBatch(@RequestBody List<Integer> ids) {
        return Result.success(ordersService.removeByIds(ids));
    }

    @GetMapping("/{id}")
    public Result findOne(@PathVariable Integer id) {
        return Result.success(ordersService.getById(id));
    }

    @GetMapping("/page")
    public Result findPage(@RequestParam String name,
                           @RequestParam Integer pageNum,
                           @RequestParam Integer pageSize) {

        QueryWrapper<Orders> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id");
        if (!"".equals(name)) {
            queryWrapper.like("name", name);
        }
        return Result.success(ordersService.page(new Page<>(pageNum, pageSize), queryWrapper));
    }
    }



